<template>
    <view>
        <view
            id="_drag_button"
            class="drag"
            :style="'left: ' + left + 'px; top:' + top + 'px;'+ buStyle"
            @touchstart="touchstart"
            @touchmove.stop.prevent="touchmove"
            @touchend="touchend"
            @click.stop.prevent="click"
            :class="{transition: isDock && !isMove }"
        >
        
            <!-- <u-image style="pointer-events: none;" width="50%" height="50%" src="../../static/icons/ewmicon.png"></u-image> -->
            <view>
                <text>{{ text }}</text>
                <u-icon name="arrow-right-double" size="24"></u-icon>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        name: 'drag-button',
        props: {
            isDock:{
                type: Boolean,
                default: false
            },
            existTabBar:{
                type: Boolean,
                default: false
            },
            text: {
                type: String,
                default: '按钮'
            }
        },
        data() {
            return {
                top:0,
                left:0,
                width: 0,
                height: 0,
                offsetWidth: 0,
                offsetHeight: 0,
                windowWidth: 0,
                windowHeight: 0,
                isMove: true,
                edge: 0,
                // text: '按钮',
                buStyle: 'border-radius: 60rpx 0 0 60rpx; border-right: none;',
            }
        },
        mounted() {
            const sys = uni.getSystemInfoSync();
            
            this.windowWidth = sys.windowWidth;
            this.windowHeight = sys.windowHeight;
            
            // #ifdef APP-PLUS
                this.existTabBar && (this.windowHeight -= 50);
            // #endif
            if (sys.windowTop) {
                this.windowHeight += sys.windowTop;
            }            
            const query = uni.createSelectorQuery().in(this);
            query.select('#_drag_button').boundingClientRect(data => {
                this.width = data.width;
                this.height = data.height;
                this.offsetWidth = data.width / 2;
                this.offsetHeight = data.height / 2;
                this.left = this.windowWidth - this.width - this.edge;
                this.top = this.windowHeight - 150;
                // this.top = 600;
            }).exec();
        },
        methods: {
            click() {
                this.$emit('btnClick');
            },
            touchstart(e) {
                this.$emit('btnTouchstart');
            },
            touchmove(e) {
                // 单指触摸
                if (e.touches.length !== 1) {
                    return false;
                }
                
                this.isMove = true;
                
                this.left = e.touches[0].clientX - this.offsetWidth;
                
                let clientY = e.touches[0].clientY - this.offsetHeight;
                // // #ifdef H5
                //     clientY += this.height;
                // // #endif
                let edgeBottom = this.windowHeight - this.height - this.edge;

                // 上下触及边界
                if (clientY < this.edge) {
                    this.top = this.edge;
                } else if (clientY > edgeBottom) {
                    this.top = edgeBottom;
                } else {
                    this.top = clientY
                }
                
            },
            touchend(e) {
                if (this.isDock) {
                    let edgeRigth = this.windowWidth - this.width - this.edge;
                    
                    if (this.left < this.windowWidth / 2 - this.offsetWidth) {
                        this.left = this.edge;
                        this.buStyle = "border-radius: 0 60rpx 60rpx 0; border-left: none;"
                    } else {
                        this.left = edgeRigth;
                        this.buStyle = "border-radius: 60rpx 0 0 60rpx; border-right: none;"
                    }
                    
                }
                
                this.isMove = false;
                
                this.$emit('btnTouchend');
            },
        }}
</script>

<style lang="scss">
    .drag {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        background-color: rgba(0, 170, 255, 1);
        box-shadow: 0 0 6rpx rgba(255, 255, 255, 0.4);
        color: #000000;
        width: 180rpx;
        height: 60rpx;
        // border-radius: 60rpx 0 0 60rpx;
        font-size: $uni-font-size-sm;
        position: fixed;
        z-index: 9999999;
        border-bottom: 5rpx solid #FFFFFF;
        border-top: 5rpx solid #FFFFFF;
        border-left: 5rpx solid #FFFFFF;
        border-right: 5rpx solid #FFFFFF;
        
        &.transition {
            transition: left .3s ease,top .3s ease;
        }
        
        text {
            // margin-top: 10rpx;
        }
    }
    
</style>
